package com.lh.project.service.impl;


import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.project.common.BaseResponse;
import com.lh.project.common.DateUtil;
import com.lh.project.common.ResultUtils;
import com.lh.project.common.UserHolder;
import com.lh.project.mapper.CommentMapper;
import com.lh.project.model.entity.Comment;
import com.lh.project.model.entity.CommentReply;
import com.lh.project.model.entity.User;
import com.lh.project.model.enums.RedisConstants;
import com.lh.project.model.enums.SystemConstants;
import com.lh.project.service.CommentRelService;
import com.lh.project.service.CommentReplyService;
import com.lh.project.service.CommentService;
import com.lh.project.service.UserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
implements CommentService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private CommentRelService commentRelService;

    @Resource
    private CommentReplyService commentReplyService;

    @Resource
    private UserService userService;
    /**
     * 该方法用于查找评论，包括一二级评论
     * @param foodId
     * @param current
     * @return
     */
    @Override
    public List<Comment> getCommentList(long foodId, int current) {
        //获取一级评论
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("food_id",foodId);
        Page<Comment> page1 = new Page<>(current, SystemConstants.DEFAULT_PAGE_SIZE);
        Page<Comment> page = page(page1, queryWrapper);
        //查出来了一个food下的所有的一级评论
        List<Comment> list = page.getRecords();
        List<Long> commentReplyList = null;
        int index = -1;
        for(Comment comment : list){
            index++;
            //获取一级评论下的用户信息
            User user = userService.getById(list.get(index).getUserId());
            list.get(index).setUser(user);
            //计算出每一个一级评论距现在的时间间隔
            Date date = new Date();
            String spaceTime = DateUtil.getDateRange(list.get(index).getCreateTime(), date);
            list.get(index).setSpaceTime(spaceTime);
            //判断该用户是否点赞
            String key = RedisConstants.COMMENT_LIKED_KEY+list.get(index).getId();
            if(stringRedisTemplate.hasKey(key)&&UserHolder.getUser()!=null){
//                System.out.println("用户是："+UserHolder.getUser().getId().toString());
                Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, UserHolder.getUser().getId().toString());
                if(isMember){
                    list.get(index).setIsLike(true);
                }else{
                    list.get(index).setIsLike(false);
                }
            }else{
                list.get(index).setIsLike(false);
            }
            //获取某一个一级评论下的二级评论
            commentReplyList = commentRelService.selectCommentReply(comment.getId());
            List<CommentReply> commentReplies =
                    //这个getByIds可以将其中的用户详细信息都映射出来
                    commentReplyService.getByIds(commentReplyList,date);
            list.get(index).setChildren(commentReplies);
        }
        return list;
    }

    @Override
    public BaseResponse<String> likeComment(Long userId, Long commentId) {
        String key = RedisConstants.COMMENT_LIKED_KEY+commentId;
        Boolean isMember = stringRedisTemplate.opsForSet().isMember(key, userId.toString());
        if(BooleanUtil.isTrue(isMember)){
            //减数据库
            boolean isSuccess = update().setSql("comment_like = comment_like-1").eq("id", commentId).update();
            if(isSuccess){
                stringRedisTemplate.opsForSet().remove(key,userId.toString());
            }
            return ResultUtils.success("取消成功");
        }else{
            boolean isSuccess = update().setSql("comment_like = comment_like+1").eq("id", commentId).update();
            if(isSuccess){
                stringRedisTemplate.opsForSet().add(key,userId.toString());
            }
            return ResultUtils.success("点赞成功");
        }
    }
}
